//------------------------------------------------
//		PRBS.c
//	Pseudo-Random Binary Sequence
//	Arthur Benemann 22/10/2012
//------------------------------------------------
//	Description:
//	Generates a pseudo random binary sequence.
//      Used for control system identification
//-----------------------------------------------

unsigned int shift_reg = 0xFFFF;
const char tap[16] = {0, 0, 0, 2, 3, 3, 5, 6, 0, 5, 7, 9, 0, 0, 0, 14};

void PRBSinit(){
    shift_reg=0xFFFF;
}

unsigned int PRBS(int size) {
    unsigned int m, n;
    m = (shift_reg >> (size - 1)) & 0x1;
    n = (shift_reg >> (tap[size] - 1)) & 0x1;
    shift_reg = (shift_reg << 1);
    if (m^n)
        shift_reg |= 0x1;
    return m;
}

